<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Test extends CI_Controller {

    function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in'))
            redirect("login");
        $this->load->model('user_model');
        $this->load->model('admin_model');
        $this->load->model('login_model');
        $this->load->model('imex_model');
        $this->load->model('update_model');
    }

    function index() {
        echo "<h2>SKaP Model Tests</h2>";

        $this->db->trans_start();
        $this->db->query('SET FOREIGN_KEY_CHECKS=0;');
        $this->db->query('TRUNCATE db_log;');
        $this->db->query('TRUNCATE room;');
        $this->db->query('TRUNCATE user_events;');
        $this->db->query('TRUNCATE event;');
        $this->db->query('TRUNCATE user_login;');
        $this->db->query('TRUNCATE user_info;');
        $this->db->query("INSERT INTO user_info (user_code, first_name, surname, department, user_type) VALUES
('ADMIN01', 'Root', 'Admin', 'Admin', 1);");
        $this->db->query("SET @pass := (SELECT SHA1('admin'));");
        $this->db->query("INSERT INTO user_login (user_id, username, PASSWORD, isAdmin) VALUES
('1', 'admin', @pass, 1);");
        $this->db->query('SET FOREIGN_KEY_CHECKS=1;');
        $this->db->query('INSERT INTO room (room_name) VALUES ("A");');
        $this->db->trans_commit();

        $result = TRUE;

        $result &= $this->loginModelTests();
        $result &= $this->updateModelTests();
        $result &= $this->imexModelTests();
        $result &= $this->userModelTests();
        $result &= $this->adminModelTests();

        if ($result) {
            echo "<br /><h2>All tests - OK</h2>";
        } else {
            echo "<br /><h2>All tests - FAILED</h2>";
        }

        $this->db->trans_start();
        $this->db->query('SET FOREIGN_KEY_CHECKS=0;');
        $this->db->query('TRUNCATE room;');
        $this->db->query('TRUNCATE user_events;');
        $this->db->query('TRUNCATE event;');
        $this->db->query('TRUNCATE user_login;');
        $this->db->query('TRUNCATE user_info;');
        $this->db->query("INSERT INTO user_info (user_code, first_name, surname, department, user_type) VALUES
('ADMIN01', 'Root', 'Admin', 'Admin', 1);");
        $this->db->query("SET @pass := (SELECT SHA1('admin'));");
        $this->db->query("INSERT INTO user_login (user_id, username, PASSWORD, isAdmin) VALUES
('1', 'admin', @pass, 1);");
        $this->db->query('SET FOREIGN_KEY_CHECKS=1;');
        $this->db->query('TRUNCATE db_log;');
        $this->db->trans_commit();
    }

    function loginModelTests() {
        $result = TRUE;

        $result &= $this->testFalse($this->login_model->verifyLogin(array("username" => "admin")), "Login - incorrect input");
        $result &= $this->testFalse($this->login_model->verifyLogin(array("username" => "admin", "password" => "xxx")), "Login - wrong login");
        $result &= $this->testTrue($this->login_model->verifyLogin(array("username" => "admin", "password" => $this->encrypt->sha1("admin"))), "Login - correct login");

        $this->printTestResult($result, "Login Model Tests");
        return $result;
    }

    function updateModelTests() {
        $result = TRUE;

        $result &= $this->testFalse($this->update_model->updateData(array()), "Update - no data");
        $result &= $this->testFalse($this->update_model->updateData(array("wrong" => "")), "Update - wrong data");
        $result &= $this->testFalse($this->update_model->updateData(array("teachers" => array(), "events" => array(), "rooms" => array())), "Update - no data");

        $data1 = array(
            'teachers' => array(array(
                    "user_code" => 1, "first_name" => 4567, "surname" => "cba", "department" => "des", "login" => "saw"
            )),
            'rooms' => array(array("room_name" => "A"
            )),
            'events' => array(array(
                    "description" => "-", "day" => 1, "start_time" => 150, "end_time" => 350, "room" => "A", "teachers_list" => array()
            ))
        );
        $result &= $this->testTrue($this->update_model->updateData($data1), "Update - good data");

        $data2 = array(
            'teachers' => array(array(
                    "useasddasr_code" => 1, "first_name" => 4567, "surname" => "cba", "department" => "des", "login" => "saw"
            )),
            'rooms' => array(array("room_name" => "A"
            )),
            'events' => array(array(
                    "description" => "-", "dsay" => 1, "start_time" => 150, "end_time" => 350, "room" => "A", "teachers_list" => array()
            ))
        );
        $result &= $this->testFalse($this->update_model->updateData($data2), "Update - wrong data");

        $this->printTestResult($result, "Update Model Tests");
        return $result;
    }

    function imexModelTests() {
        $result = true;

        $result &= $this->testFalse($this->imex_model->importEvents(array()), "Import - no data");

        $data1 = array(
            'user' => 1,
            'events' => array(array('event_type' => 'výučba', 'description' => "-", 'day' => "1", 'start_time' => "123", 'end_time' => "321", 'room' => "A"))
        );
        $result &= $this->testTrue($this->imex_model->importEvents($data1), "Import - correct data");

        $data2 = array(
            'events' => array(array('event_type' => 'výučba', 'description' => "NULL", 'day' => 1, 'start_time' => 123, 'end_time' => 321, 'room' => "As"))
        );
        $result &= $this->testFalse($this->imex_model->importEvents($data2), "Import - incorrect data");

        $expRes1 = array(array(
                'event_type' => 'výučba',
                'description' => '-',
                'day' => '1',
                'start_time' => '123',
                'end_time' => '321',
                'room' => 'A',
                'teachers_list' => 'Root Admin'));
        $this->testEqual($expRes1, $this->imex_model->exportEvents(array()), "Export");

        $this->testEqual(array(), $this->imex_model->exportEvents(array("event" => array(12))), "Export");

        $this->printTestResult($result, "ImEx Model Tests");
        return $result;
    }

    function userModelTests() {
        $result = true;

        $addEvent1 = array(
            'user_id' => 1,
            'event' => array(
                'event_type' => 2,
                'event_day' => '2',
                'start_time' => 850,
                'end_time' => 885,
                'room_id' => 1
            )
        );
        $result &= $this->testTrue($this->user_model->addEvent($addEvent1), "Add event");

        $editEvent1 = array(
            'event_id' => 1,
            'event' => array('event_day' => '5')
        );
        $result &= $this->testTrue($this->user_model->editEvent($editEvent1), "Edit event");

        $this->testEqual(3, count($this->user_model->getEvents(array())), "Get events");
        $this->testEqual(array(), $this->user_model->getEvents(array('room_id' => array(21))), "Get events");

        $deleteEvent1 = array(
            'event_id' => 2,
            'user_id' => 1
        );
        $result &= $this->testTrue($this->user_model->deleteEvent($deleteEvent1), "Delete event");

        $addEvent2 = array(
            'user_id' => 21,
            'event' => array(
                'event_type' => 2,
                'event_day' => '2',
                'start_time' => 850,
                'end_time' => 885,
                'room_id' => 1
            )
        );
        $result &= $this->testFalse($this->user_model->addEvent($addEvent2), "Add event");

        $editEvent2 = array(
            'event_id' => 221,
            'event' => array('event_day' => '5')
        );
        $result &= $this->testFalse($this->user_model->editEvent($editEvent2), "Edit event");

        $deleteEvent2 = array(
            'event_id' => 2,
            'user_id' => 12
        );
        $result &= $this->testFalse($this->user_model->deleteEvent($deleteEvent2), "Delete event");

        $this->printTestResult($result, "User Model Tests");
        return $result;
    }

    function adminModelTests() {
        $result = true;
        $this->db->set_trans_status(true);
        $addUser1 = array(
            'user_info' => array("first_name" => "Janko", "surname" => "Anonym", "department" => "DAI", "user_type" => 1),
            'user_login' => array("username" => "janko009", "password" => "asdfdksjhqwqasd", "isAdmin" => 0)
        );
        $result &= $this->testTrue($this->admin_model->addUser($addUser1), "Add user");

        $editUser1 = array(
            'user_id' => 3,
            'user_info' => array("first_name" => "Janicko"),
        );
        $result &= $this->testTrue($this->admin_model->editUser($editUser1), "Edit user");

        $deleteUser1 = array("user_id" => 3);
        $result &= $this->testTrue($this->admin_model->deleteUser($deleteUser1), "Delete user");

        $this->testEqual(2, count($this->admin_model->getUsers(array())), "Get all users");
        $this->testEqual(array(), $this->admin_model->getUsers(array('user_id' => array(21))), "Get user");

        $addUser2 = array(
            'user_info' => array("first_name" => "Janko", "surname" => "Anonym", "department" => "DAI", "user_type" => 4),
            'user_login' => array("username" => "janko009", "password" => "asdfdksjhqwqasd", "isAdmin" => 0)
        );
        $result &= $this->testFalse($this->admin_model->addUser($addUser2), "Add user");

        $editUser2 = array(
            'user_id' => 3,
            'user_info' => array("first_name" => "Janicko"),
        );
        $result &= $this->testFalse($this->admin_model->editUser($editUser2), "Edit user");

        $deleteUser2 = array("user_id" => 3);
        $result &= $this->testFalse($this->admin_model->deleteUser($deleteUser2), "Delete user");

        $this->db->set_trans_status(true);
        $result &= $this->testEqual(31, count($this->admin_model->getLog(array())), "Get log");
        $result &= $this->testEqual(array(), $this->admin_model->getLog(array('user_id' => array(123, 312))), "Get log");

        $this->printTestResult($result, "Admin Model Tests");
        return $result;
    }

    function printTestResult($result, $test) {
        echo "------------> <b>" . $test . "</b>:  - ";
        if ($result) {
            echo "OK<br /><br />";
        } else {
            echo "FALSE<br /><br />";
        }
    }

    function testEqual($obj1, $obj2, $test) {
        if ($obj1 == $obj2) {
            echo "<br />" . $test . ":";
            echo var_dump($obj1) . "==";
            echo var_dump($obj2) . "<br /> - OK<br /><br />";
            return true;
        } else {
            echo $test . ":" . var_dump($obj1) . "'=='";
            echo var_dump($obj2) . "<br /> - FALSE<br /><br />";
            return false;
        }
    }

    function testTrue($value, $test) {
        if ($value) {
            echo $test . " - OK<br />";
            return true;
        } else {
            echo $test . " - FALSE<br />";
        }
    }

    function testFalse($value, $test) {
        return $this->testTrue(!$value, $test);
    }

}

?>
